package com.spring.demo.web.controller;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.spring.demo.core.entity.Member;

/**
 * rest demo
 * 
 * @author sean
 * 
 */
@Controller
@RequestMapping("/rest")
public class RestController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private Member member;

	@PostConstruct
	public void postConstruct() {
		member = new Member();
		member.setEmail("657620636@qq.com");
		member.setName("张三");
		member.setStreet("南山区白石洲天河街55号");
		member.setZip("518025");
	}

	/**
	 * 输出xml
	 * 
	 * @return
	 */
	@RequestMapping(value = "/xml", produces = { MediaType.APPLICATION_XML_VALUE })
	@ResponseBody
	public Member xml() {
		logger.debug("rest xml");
		return member;
	}

	/**
	 * 输出json
	 * 
	 * @return
	 */
	@RequestMapping(value = "/json", produces = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public Member json() {
		logger.debug("rest json");
		return member;
	}

	/**
	 * 更新操作，只需要返回成功与否的状态。
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateAndReturnOk")
	@ResponseBody
	public String updateAndReturnOk() {
		logger.debug("rest updateAndReturnOk");

		return "Ok";
	}

	/**
	 * 更新操作，什么都不返回。
	 */
	@RequestMapping(value = "/update")
	@ResponseStatus(value = HttpStatus.NO_CONTENT)
	@ResponseBody
	public void update() {
		logger.debug("rest update and return no content");
	}
}
